package com.ypf.rpc.nettyHandler;


import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ChannelHandler.Sharable
public class HeartBeatServerHandler extends ChannelDuplexHandler {
    /**
     * 读空闲
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent){
            IdleState state = ((IdleStateEvent) evt).state();
            // 读空闲事件
            if (state==IdleState.READER_IDLE){
                log.info("长时间未收到心跳包，断开连接...");
                ctx.close();
            }
        }else {
            super.userEventTriggered(ctx,evt);
        }
    }
}
